package com.zui.util;

import java.util.Iterator;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

/**
 * For jqGrid jquery plugin paging grid. prmNames : { page : "jq.requestPage",
 * rows : "jq.pageSize", sort : "jq.sortColumn", order : "jq.sortDirection",
 * search : "jq._search", nd : "jq.nd", npage : null }
 * 
 */
public class JqGridPage extends Page {
	// TODO: 这个已经不是page了，而是前台传来的所有查询条件；

	public JqGridPage() {
		super();
		super.type = TYPE_JQGRID;
	}

	private static final long serialVersionUID = 1L;

	/**
	 * the search indicator for jqGrid
	 */
	private Boolean _search;
	/**
	 * the operation parameter: edit, add, del
	 */
	private JQGridOper oper;

	public enum JQGridOper {
		add, edit, del
	};

	private String id;
	private String rowids;
	/**
	 * the time passed to the request (for IE browsers not to cache the request)
	 * - default value nd
	 */
	private String nd;

	private String searchField;

	/**
	 * Use this option to set common search rules. If not set all the available
	 * options will be used. All available option are:
	 * ['eq','ne','lt','le','gt','ge','bw','bn','in','ni','ew','en','cn','nc']
	 * The corresponding texts are in language file and mean the following:
	 * ['equal','not equal', 'less', 'less or equal','greater','greater or
	 * equal', 'begins with','does not begin with','is in','is not in','ends
	 * with','does not end with','contains','does not contain'] Note that the
	 * elements in sopt array can be mixed in any order.
	 */
	public enum JQGridSearchOper {
		eq, ne, lt, le, gt, ge, bw, bn, in, ni, ew, en, cn, nc
	};

	private JQGridSearchOper searchOper;
	private String searchString;
	private String searchFilter;
	
	//	Criterion
	public Criterion getCriterion(JQGridFilterRule rule) {
		return getCriterion(rule.getField(), JQGridSearchOper.valueOf(rule.getOp()), rule.getData());
	}
	//	Criterion
	public Criterion getCriterion(String field,
			JQGridSearchOper op, String data) {
		if (op == null)
			return null;
		
		Criterion cri = null;
		switch (op) {
		case eq:cri = Restrictions.eq(field, data);break;
		case ne:cri = Restrictions.ne(field, data);break;
		case lt:cri = Restrictions.lt(field, data);break;
		case le:cri = Restrictions.le(field, data);break;
		case gt:cri = Restrictions.gt(field, data);break;
		case ge:cri = Restrictions.ge(field, data);break;
		case bw:cri = Restrictions.like(field, data, MatchMode.START);break;
		case bn:cri = Restrictions.not(Restrictions.like(field, data,
					MatchMode.START));break;
		// case in : criteria.add(Restrictions.in(field, data));break;
		// case ni : criteria.add(Restrictions.(field, data));break;
		case ew:cri = Restrictions.like(field, data, MatchMode.END);break;
		case en:cri = Restrictions.not(Restrictions.like(field, data,
					MatchMode.END));break;
		case cn:cri = Restrictions.like(field, data, MatchMode.ANYWHERE);break;
		case nc:cri = Restrictions.not(Restrictions.like(field, data,
					MatchMode.ANYWHERE));break;
		default:
			break;
		}
		return cri;
	}
	public void setCriteria(DetachedCriteria criteria, String field,
			JQGridSearchOper op, String data) {
		if (op == null)
			return;
		criteria.add(getCriterion(field, op, data));
	}

	/**
	 * For single column jqgrid search, using "searchField", "searchOper", "searchString" in JqGrdiPage Object.
	 * @param criteria
	 */
	public void setCriteria(DetachedCriteria criteria) {
		this.setCriteria(criteria, searchField, searchOper, searchString);
	}

	/**
	 * Contains association query.
	 * @param criteria
	 * @param rule
	 */
	public void setCriteria(DetachedCriteria criteria, JQGridFilterRule rule) {
		
		//Set Association Criteria firstly.
		if (rule.isAssociation()) {
			String[] hieFields = rule.getHierarchyFields();
			
			for(String hie:hieFields) {
				criteria = criteria.createCriteria(hie);
			}
		}
		
		this.setCriteria(criteria, rule.getField(),
				JQGridSearchOper.valueOf(rule.getOp()), rule.getData());
	}

	/**
	 * 
	 * 
	 * @param criteria
	 * @param filters
	 *            {"groupOp":"AND","rules":[{"field":"catalogName","op":"bw",
	 *            "data":"2"}]}
	 */
	public void setGroupCriteria(DetachedCriteria criteria, JQGridFilter filter) {
		//when nothing is input, you will get null rules
		if (filter == null || filter.getRules() == null)
			return;
		if (filter.getRules() != null && filter.getRules().size() == 1) {
			JQGridFilterRule rule = filter.getRules().get(0);
			this.setCriteria(criteria, rule);
		} else if (filter.getGroupOp().equalsIgnoreCase("AND")) {
			Iterator<JQGridFilterRule> it = filter.getRules().iterator();
			while (it.hasNext()) {
				JQGridFilterRule rule = it.next();
				setCriteria(criteria, rule);
			}
		} else if (filter.getGroupOp().equalsIgnoreCase("OR")) {
			Iterator<JQGridFilterRule> it = filter.getRules().iterator();
			Criterion lhs = null;
			while (it.hasNext()) {
				JQGridFilterRule rule = it.next();
				if (lhs == null) {
					lhs = getCriterion(rule);
					continue;
				} else 
					lhs = Restrictions.or(lhs, getCriterion(rule));
			}
			criteria.add(lhs);
		}
	}

	/*******************************
	 * getter and setter
	 ********************************/
	public Boolean get_search() {
		return _search;
	}

	public void set_search(Boolean search) {
		_search = search;
	}

	public String getNd() {
		return nd;
	}

	public void setNd(String nd) {
		this.nd = nd;
	}

	public String getSearchField() {
		return searchField;
	}

	public void setSearchField(String searchField) {
		this.searchField = searchField;
	}

	public JQGridSearchOper getSearchOper() {
		return searchOper;
	}

	public void setSearchOper(JQGridSearchOper searchOper) {
		this.searchOper = searchOper;
	}

	public String getSearchString() {
		return searchString;
	}

	public void setSearchString(String searchString) {
		this.searchString = searchString;
	}

	public JQGridOper getOper() {
		return oper;
	}

	public void setOper(JQGridOper oper) {
		this.oper = oper;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getRowids() {
		return rowids;
	}

	public void setRowids(String rowids) {
		this.rowids = rowids;
	}

	public String getSearchFilter() {
		return searchFilter;
	}

	public void setSearchFilter(String searchFilter) {
		this.searchFilter = searchFilter;
	}

}
